package com.erp.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.erp.shop.domain.ErpShopBindRecord;
import com.erp.shop.domain.bo.ErpShopBindRecordBo;
import com.erp.shop.domain.vo.ErpShopBindRecordVo;
import com.erp.shop.mapper.ErpShopBindRecordMapper;
import com.erp.shop.service.IErpShopBindRecordService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 店铺绑定记录Service业务层处理
 *
 * @author erp
 * @date 2024-05-07
 */
@RequiredArgsConstructor
@Service
public class ErpShopBindRecordServiceImpl implements IErpShopBindRecordService {

    private final ErpShopBindRecordMapper baseMapper;

    /**
     * 查询店铺绑定记录
     */
    @Override
    public ErpShopBindRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺绑定记录列表
     */
    @Override
    public TableDataInfo<ErpShopBindRecordVo> queryPageList(ErpShopBindRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpShopBindRecord> lqw = buildQueryWrapper(bo);
        Page<ErpShopBindRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺绑定记录列表
     */
    @Override
    public List<ErpShopBindRecordVo> queryList(ErpShopBindRecordBo bo) {
        LambdaQueryWrapper<ErpShopBindRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpShopBindRecord> buildQueryWrapper(ErpShopBindRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpShopBindRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStoreId() != null, ErpShopBindRecord::getStoreId, bo.getStoreId());
        lqw.like(StringUtils.isNotBlank(bo.getStoreUserName()), ErpShopBindRecord::getStoreUserName, bo.getStoreUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), ErpShopBindRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getParentAgentId() != null, ErpShopBindRecord::getParentAgentId, bo.getParentAgentId());
        lqw.eq(bo.getParentBranchId() != null, ErpShopBindRecord::getParentBranchId, bo.getParentBranchId());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpShopBindRecord::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增店铺绑定记录
     */
    @Override
    public Boolean insertByBo(ErpShopBindRecordBo bo) {
        ErpShopBindRecord add = BeanUtil.toBean(bo, ErpShopBindRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺绑定记录
     */
    @Override
    public Boolean updateByBo(ErpShopBindRecordBo bo) {
        ErpShopBindRecord update = BeanUtil.toBean(bo, ErpShopBindRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpShopBindRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店铺绑定记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
